<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .box {
            margin: 100px auto;
            width: 500px;
        }
        
        textarea {
            width: 200px;
            height: 100px;
            border: 1px solid pink;
            color: rosybrown;
            font-size: 14px;
        }
        
        button {
            width: 40px;
            height: 25px;
        }
        
        li {
            margin-bottom: 10px;
            background-color: pink;
            /* list-style-type: none; */
            height: 30px;
            line-height: 30px;
            color: red;
        }
        
        li a {
            float: right;
            text-decoration: none;
            color: skyblue;
            font-size: 14px;
            padding-right: 10px;
        }
        
        ul {
            width: 300px;
            padding-top: 40px;
        }
    </style>
</head>

<body>
    <div class="box">
        <textarea name="" id="" cols="30" rows="10"></textarea>
        <button>发布</button>
        <ul>
        </ul>
    </div>
    <script>
        var ul = document.querySelector('ul');
        var btn = document.querySelector('button');
        var text = document.querySelector('textarea');
        btn.onclick = function() {
            if (text.value.length === 0) {
                alert('没有输入内容');
            } else {
                var li = document.createElement('li');
                // href='javascript:;'可以阻止链接跳转
                li.innerHTML = text.value + "<a href='javascript:;'>删除</a>";
                // ul.appendChild(li);
                ul.insertBefore(li, ul.children[0]);

                // 给删除链接添加点击事件
                var as = document.querySelector('.box').querySelectorAll('a');
                for (let i = 0; i < as.length; i++) {
                    as[i].onclick = function() {
                        // 删除节点 this.parentNode 代表点前点击a标签的父节点
                        ul.removeChild(this.parentNode);
                    }
                }
            }
        }
    </script>
</body>

</html>